package com.example.springbootredis.controller;

import com.example.springbootredis.entity.Classroom;
import com.example.springbootredis.service.ClassroomService;
import com.example.springbootredis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("classroom")
@CrossOrigin
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;

    @PostMapping("save")
    public JsonResult save(@RequestBody Classroom classroom) {
        // 必填验证
        if (classroom.getClassroomNo() == null || classroom.getClassroomNo().isEmpty()) {
            return JsonResult.failed("教室编号不能为空");
        }
        if (classroom.getCampus() == null || classroom.getCampus().isEmpty()) {
            return JsonResult.failed("校区不能为空");
        }
        if (classroom.getClassroomName() == null || classroom.getClassroomName().isEmpty()) {
            return JsonResult.failed("教室名称不能为空");
        }
        if (classroom.getClassroomAddress() == null || classroom.getClassroomAddress().isEmpty()) {
            return JsonResult.failed("教室地址不能为空");
        }

        // 检查教室编号是否重复
        if (classroom.getId() == null) {
            if (classroomService.existsByClassroomNo(classroom.getClassroomNo())) {
                return JsonResult.failed("教室编号已存在");
            }
        } else {
            if (classroomService.existsByClassroomNoAndIdNot(classroom.getClassroomNo(), classroom.getId())) {
                return JsonResult.failed("教室编号已存在");
            }
        }

        Classroom res = classroomService.save(classroom);
        return res != null ? JsonResult.success("操作成功", null) : JsonResult.failed("操作失败");
    }

    // 教室列表
    @GetMapping("list")
    public JsonResult list() {
        List<Classroom> list = classroomService.findAll();
        return JsonResult.success("获取成功", list);
    }

    // 删除教室
    @GetMapping("delete")
    public JsonResult delete(@RequestParam Long id) {
        classroomService.deleteClassroomById(id);
        return JsonResult.success("删除成功", null);
    }

    // 根据校区筛选
    @GetMapping("byCampus")
    public JsonResult findByCampus(@RequestParam String campus) {
        List<Classroom> list = classroomService.findByCampus(campus);
        return JsonResult.success("获取成功", list);
    }

    // 根据教室名模糊查询
    @GetMapping("byName")
    public JsonResult findByName(@RequestParam String name) {
        List<Classroom> list = classroomService.findByClassroomNameContaining(name);
        return JsonResult.success("获取成功", list);
    }

    // 查询
    @GetMapping("search")
    public JsonResult search(@RequestParam(required = false) String campus,
                             @RequestParam(required = false) String name) {
        List<Classroom> list;
        if (campus != null && name != null) {
            list = classroomService.findByCampusAndClassroomNameContaining(campus, name);
        } else if (campus != null) {
            list = classroomService.findByCampus(campus);
        } else if (name != null) {
            list = classroomService.findByClassroomNameContaining(name);
        } else {
            list = classroomService.findAll();
        }
        return JsonResult.success("获取成功", list);
    }
}